package gov.nist.applet.phone.security.filetransfer;

import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.Rectangle;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class FileTransferDialog extends JDialog
{

	private static final long serialVersionUID = 1L;
	private JPanel jContentPane = null;
	private JPanel jPanel = null;
	private JLabel jLabel = null;
	private JTextField jTextField = null;
	private JButton jButton = null;
	private JButton jButton1 = null;
	private JButton jButton2 = null;
	
	private ReceiveFile receive = null;  //  @jve:decl-index=0:
	private SendFile send = null;  //  @jve:decl-index=0:
	private String path = null;


	
	


	public void setReceive(ReceiveFile receive)
	{
		this.receive = receive;
	}

	public void setSend(SendFile send)
	{
		this.send = send;
	}

	/**
	 * This method initializes jPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	
	
	private JPanel getJPanel()
	{
		if (jPanel == null)
		{
			jLabel = new JLabel();
			jLabel.setBounds(new Rectangle(38, 22, 83, 28));
			jLabel.setText("请选择文件路径");
			jPanel = new JPanel();
			jPanel.setLayout(null);
			jPanel.add(jLabel, null);
			jPanel.add(getJTextField(), null);
			jPanel.add(getJButton(), null);
			jPanel.add(getJButton1(), null);
			jPanel.add(getJButton2(), null);
		}
		return jPanel;
	}

	/**
	 * This method initializes jTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField()
	{
		if (jTextField == null)
		{
			jTextField = new JTextField();
			jTextField.setBounds(new Rectangle(137, 22, 162, 31));
		}
		return jTextField;
	}

	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton()
	{
		if (jButton == null)
		{
			jButton = new JButton();
			jButton.setBounds(new Rectangle(308, 21, 68, 30));
			jButton.setText("浏览");
			jButton.addActionListener(new java.awt.event.ActionListener()
			{
				public void actionPerformed(java.awt.event.ActionEvent e)
				{
					JFileChooser chooser = new JFileChooser();
					chooser.setCurrentDirectory(new File("."));
					int result = chooser.showSaveDialog(null);
					if(result==JFileChooser.APPROVE_OPTION)
					{
						path = chooser.getSelectedFile().getPath();
						//返回改变的新的字符串
						path = path.replace("\\","/");
					}
					else
						return;
					
					FileTransferDialog.this.jTextField.setText(path);
				}
			});
		}
		return jButton;
	}

	/**
	 * This method initializes jButton1	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton1()
	{
		if (jButton1 == null)
		{
			jButton1 = new JButton();
			jButton1.setBounds(new Rectangle(87, 92, 77, 32));
			jButton1.setText("发送");
			jButton1.addActionListener(new java.awt.event.ActionListener()
			{   
				public void actionPerformed(java.awt.event.ActionEvent e) 
				{
	
					// it's a sender
					if(send!=null)
					{
						//String path = FileTransferDialog.this.jTextField.getText();
						if(path==null)
							return;
						send.setFilePath(path);
						send.start();
						setVisible(false);
						dispose();
					}

				}
			});
		}
		return jButton1;
	}

	/**
	 * This method initializes jButton2	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButton2()
	{
		if (jButton2 == null)
		{
			jButton2 = new JButton();
			jButton2.setBounds(new Rectangle(211, 89, 76, 36));
			jButton2.setText("接收");
			jButton2.addActionListener(new java.awt.event.ActionListener()
			{
				public void actionPerformed(java.awt.event.ActionEvent e)
				{
					//it's a receiver
					if(receive!=null)
					{
						String path = FileTransferDialog.this.jTextField.getText();
						if(path==null)
							return;
						receive.setSavePath(path);
						receive.receive();
						setVisible(false);
						dispose();
					}
				}
			});
		}
		return jButton2;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args)
	{
		FileTransferDialog dialog = new FileTransferDialog(null);
		dialog.setVisible(true);
		
		ReceiveFile receiver = new ReceiveFile("localhost");
		SendFile sender = new SendFile();
		
		//dialog.setSend(sender);
		
		dialog.setReceive(receiver);
		

		
	}

	/**
	 * @param owner
	 */
	public FileTransferDialog(Frame owner)
	{
		super(owner);
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize()
	{
		this.setSize(400, 210);
		this.setContentPane(getJContentPane());
		this.addWindowListener(new WindowAdapter()
		{
			public void windowClosing(WindowEvent e)
			{
				FileTransferDialog.this.setVisible(false);
				FileTransferDialog.this.dispose();
			}
		});
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane()
	{
		if (jContentPane == null)
		{
			jContentPane = new JPanel();
			jContentPane.setLayout(new BorderLayout());
			jContentPane.add(getJPanel(), BorderLayout.CENTER);
		}
		return jContentPane;
	}

}  //  @jve:decl-index=0:visual-constraint="192,-137"
